﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Library;
using Microsoft.Advertising.Mobile.UI;
using Microsoft.Phone.Net.NetworkInformation;
using OnlineVideos.Library;
using OnlineVideos.Common;
using OnlineVideos.Entities;
using OnlineVideos.UI;
using OnlineVideos.Views;
using Common.Utilities;
using Common;
using OnlineVideos.Data;
using Microsoft.Phone.Controls;


namespace OnlineVideos
{
    public partial class PersonGallery : UserControl
    {
        #region GlobalDeclaration
        private bool IsDataLoaded;
        int gallerycount, galleryeventcount;
        Size galleryImageSize;
        #endregion
        #region Constructor
        public PersonGallery()
        {
            InitializeComponent();
            IsDataLoaded = false;
            gallerycount = 0;
            galleryeventcount = 0;

           
        }

        #endregion

        #region PageLoad

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
            LoadAds();
            if (IsDataLoaded == false)
            {
                 LoadGallery();
                IsDataLoaded = true;
            }
        }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In PersonGallery.cs file.", ex);
            }
        }

        #endregion

        #region Comman Methods
        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(LayoutRoot, adstackpl, 2);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In SongDetails file", ex);
                string excepmess = "Exception in LoadAds Method In SongDetails file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }

        }
        private void LoadGallery()
        {
            if (NetworkHelper.IsNetworkAvailable())
            {
                _performanceProgressBargallery.IsIndeterminate = true;
                lbxGallery.ItemsSource = OnlineShow.GetPersonGallery(AppSettings.PersonUniqueID);
                AppSettings.gallcount = lbxGallery.Items.Count.ToString();
                if (lbxGallery.Items.Count > 0)
                {
                    lbxGallery.Loaded += new RoutedEventHandler(lbxGallery_Loaded);

                    tblkGallery.Visibility = Visibility.Collapsed;
                    _performanceProgressBargallery.IsIndeterminate = false;
                }
                else
                {
                    tblkGallery.Text = "Images are Not Available";
                    tblkGallery.Visibility = Visibility.Visible;
                    _performanceProgressBargallery.IsIndeterminate = false;
                }

            }
            else
            {
                tblkGallery.Text = "Network Not Available";
                tblkGallery.Visibility = Visibility.Visible;
            }
        } 
        #endregion
        #region Events
        private void lbxGallery_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbxGallery.SelectedIndex != -1)
            {
                AppSettings.GalleryTitle = (lbxGallery.SelectedItem as GalleryImageInfo).Title;
                PageHelper.NavigateToPersonGalleryPopupPage(AppResources.PersonGalleryPopupPage);
            }
            lbxGallery.SelectedIndex = -1;
        }

        void lbxGallery_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                IEnumerable<DependencyObject> cboxes = PageHelper.GetChildsRecursive(lbxGallery);
                //TODO: verify if this code can be reused
                foreach (DependencyObject obj in cboxes.OfType<Image>())
                {
                    Type type = obj.GetType();
                    if (type.Name == "Image")
                    {
                        Image cb = obj as Image;
                        if (cb.Source != null)
                        {

                            if (cb.RenderSize != galleryImageSize)
                            {
                                cb.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(cb_ImageFailed);

                                cb.SizeChanged += new SizeChangedEventHandler(cb_SizeChanged);
                                gallerycount++;
                            }
                            else
                            {
                                _performanceProgressBargallery.IsIndeterminate = false;
                            }
                        }
                        else
                        {
                            _performanceProgressBargallery.IsIndeterminate = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                
                   Exceptions.SaveOrSendExceptions("Exception in lbxGallery_Loaded Method In PersonGallery.cs file.", ex);
            }
        }

        void cb_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            try
            {
            _performanceProgressBargallery.IsIndeterminate = false;
        }
            catch (Exception ex)
            {
                
                Exceptions.SaveOrSendExceptions("Exception in cb_ImageFailed Method In PersonGallery.cs file.", ex);
            }
        }

        void cb_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            galleryeventcount++;
            galleryImageSize = e.NewSize;

            if (galleryeventcount == gallerycount)
                _performanceProgressBargallery.IsIndeterminate = false;
        }
        #endregion
    }
}
